<?php
include_once "function_misc.php";
include_once "function_xml_basic.php";


#-------------------------------- System Command ------------------------------------
function GetNetDevList()
{
	$command_string = '/usr/local/bin/netstat -ai | grep eth[0-9]| awk \'{print $1}\' | grep -v ":" | grep -v "\."';
	$ret_string = shell_exec($command_string);
	$ret_array = explode("\n", $ret_string);
	for ($i = 0; $i < count($ret_array); $i ++ )
	{
		if ($ret_array[$i] == '')
			unset($ret_array[$i]);
	}
	return $ret_array;
}
#$ret_array = GetNetDevList();
#foreach ($ret_array as $ret)
#{
#	print $ret;
#}

function GetVlanDevList()
{
	$command_string = '/sbin/ifconfig -a | grep eth[0-9] | awk \'{print $1}\' | grep "\."';
	$ret_string = shell_exec($command_string);
	$ret_array = explode("\n", $ret_string);
	for ($i = 0; $i < count($ret_array); $i ++ )
	{
		if ($ret_array[$i] == '')
			unset($ret_array[$i]);
	}
	return $ret_array;
}

/*
function GetAllNetDevList()
{
	$command_string = "/sbin/ifconfig -a | grep 'Link encap' | awk '{print $1}' | grep -v : | grep eth[0-9]";
	$ret_string = shell_exec($command_string);
	$ret_array = explode("\n", $ret_string);
	for ($i = 0; $i < count($ret_array); $i ++ )
	{
		if ($ret_array[$i] == '')
			unset($ret_array[$i]);
	}
	return $ret_array;
}*/

function GetNetDevMtu($dev)
{
	$command_string = '/usr/local/bin/netstat -ai | grep '. $dev. '| grep -v : | grep -v "\." | awk \'{print $2}\' ';
	$ret_string = shell_exec($command_string);
	return $ret_string;
}

function GetNetDevFlg($dev)
{
	$command_string = '/usr/local/bin/netstat -ai | grep '. $dev. '| awk \'{print $12}\'';
	$ret_string = shell_exec($command_string);
	return $ret_string;
}

function GetNetDevRxTx($dev)
{
	$command_string = '/sbin/ifconfig '. $dev. ' | grep bytes ';
	$ret_string = shell_exec($command_string);
	$ret_array = explode(":",$ret_string);
	$ret_array[1] = str_replace("TX bytes","",$ret_array[1]);
	
	$ret_array['RX'] = $ret_array[1];
	$ret_array['TX'] = $ret_array[2];

	unset($ret_array[0]);
	unset($ret_array[1]);
	unset($ret_array[2]);

	return $ret_array;
}

function GetNetDevMac($dev)
{
	$command_string = "/sbin/ifconfig ". $dev. " | grep HWaddr | awk '{printf $5}'";
	$ret_string = shell_exec($command_string);
	if ($ret_string =="")
		$ret_string = "";
	return $ret_string;
}

function GetNetDevSpeed($dev)
{
	$command_string = '/usr/local/sbin/ethtool '. $dev. ' | grep Speed: | awk \'{printf $2} \'';
	$ret_string = shell_exec($command_string);
	if ($ret_string == 'Unknown!' )
		$ret_string = "100Mb/s";
	return $ret_string;
		
}

function GetNetDevRunStatus($dev)
{
	$command_string = '/sbin/ifconfig  -a | grep ^'. $dev. ' -A 2 | grep BROADCAST |awk \'{print $1}\'';
	$ret_string = trim(shell_exec($command_string));
	if (strstr($ret_string,"UP"))
		$ret = "UP";
	else if (strstr($ret_string,"BROADCAST"))
		$ret = "DOWN";
	else
		$ret = "Unknown";

	return $ret;
}

function GetNetDevEnableStatus($dev)
{
	//$command_enable = '/sbin/ifconfig '. $dev. ' | grep "inet addr"';
	$command_get_addr = "/sbin/ifconfig ". $dev. " | grep 'inet addr' | awk '{printf $2}' | sed 's/addr://g'";
	$ret = shell_exec($command_get_addr);
	
	if (strlen($ret) < 7)
		return 1;
	else
		return 0;

	/*if ($ret == 0) //enabled
	{
		$command_get_addr = "/sbin/ifconfig ". $dev. " | grep 'inet addr' | awk '{printf $2}' | sed 's/addr://g'";
		$ret_addr = shell_exec($command_get_addr);
		return $ret_addr;
	}
	
	return 1; */
	return $ret ;
}

function GetNetDevLinkStatus($dev)
{
	$command_string = '/usr/local/sbin/ethtool '. $dev. ' | grep \'Link detected:\' | awk \'{ printf $3 }\' ';
	$ret_string = shell_exec($command_string);
	
	return strtoupper($ret_string);
}

function GetNetDevLinkMode($dev)
{
	$command_string = '/usr/local/sbin/ethtool '. $dev. ' | grep \'Duplex:\' | awk \'{printf $2}\'';
	$ret_string = shell_exec($command_string);

	if ($ret_string == 'Unknown!' )
		$ret_string = NULL;
	return $ret_string;
}

function GetNetDevIpInfo($dev)
{
	$command_string = 'ifconfig '. $dev. ' | grep "inet addr" | awk \'{printf $2}\' | awk -F: \'{print $2}\' ';
	$ret_array['IP'] = shell_exec($command_string);
	
	$command_string = 'ifconfig '. $dev. ' | grep "inet addr" | awk \'{printf $4}\' | awk -F: \'{print $2}\' ';
	$ret_array['MASK'] = shell_exec($command_string);

	return $ret_array;
	
}
function GetNetDevNegoMode($dev)
{
	$command_string = '/usr/local/sbin/ethtool eth0 | grep "Supports auto-negotiation" | awk \'{printf $3}\' ';
	$ret_string = shell_exec($command_string);
	return $ret_string;
}
function GetNetRouteInfo()
{
	#$command_string = "/sbin/route -n | /bin/sed /Kernel/d | /bin/sed /Destination/d ";
	$command_string = "/sbin/route -n | /bin/sed /Kernel/d ";
	$ret_string = MiddleTrim(shell_exec($command_string));
	$ret_arrays = explode("\n", $ret_string);
	
	return $ret_arrays;
}

include_once "function_net_bridge.php";

function GetNetDevBridgeInfo($dev)
{
	$br_list = GetBridgeDevListFromSys();

	foreach ($br_list as $br)
	{
		$br_eth_list = GetBridgeDevInfoFromSys($br);
		
		if(array_search ($dev, $br_eth_list) === FALSE ) {
			continue;
		}
		else {
			return $br;
		}
	}
	return FALSE;
}

function GetNetDevBridgeInfoByEth($dev)
{
	$br_list = GetBridgeDevListFromSys();

	foreach ($br_list as $br)
	{
		$br_eth_list = GetBridgeDevInfoFromSys($br);
		foreach ($br_eth_list as $e_list){
			if (strstr($e_list, $dev) === FALSE)
				continue;
			else
				return TRUE;
		}	
	}
	return FALSE;
}


#-------------------------------- Operate XML Tree ------------------------------------
function GetXmlNetDevInterfaceInfo($dev, $attr)
{
	$query_string = "/MINI/NETWORK/INTERFACES/INTERFACE[@Name='". $dev. "']";
	$results = GetAllNodeValue($query_string);

	$result = $results->item(0);
	if ($result == NULL)
	{	
		return NULL;
	}	
	return  $result->getAttribute($attr);
}

function GetXmlNetDevAddressInfo($dev)
{
	$ret_array = array();
	$query_string = "/MINI/NETWORK/INTERFACES/INTERFACE[@Name='". $dev. "']/ADDRESSES/ADDRESS";
	$results = GetAllNodeValue($query_string);

	$i = 0;
	foreach($results as $r){
		$ret_array[$i]["Ip"] = $r -> getAttribute('Ip'); 
		$ret_array[$i]["Mask"] = $r -> getAttribute('Mask'); 
		$ret_array[$i]["Enabled"] = $r -> getAttribute('Enabled'); 
		$i ++;
	}
	//print_r($ret_array);
	return $ret_array;
}

function GetXmlNetDevArray()
{
	$ret_array = array();
	$query_string = $query_string = "/MINI/NETWORK/INTERFACES/INTERFACE[@Type='phyInterface']";
	$results = GetAllNodeValue($query_string);

	$i = 0;
	foreach ($results as $result)
	{
		$ret_array[$i ++] = $result->getAttribute('Name');
	}
	return $ret_array;
}
#$ret_array = GetXmlNetDevArray();
#foreach ($ret_array as $ret)
#	print "[". $ret. "]\n";

function GetXmlNetDevInfo($dev)
{
	$ret_array = array();
	$edit_key1 = array ('Enabled', 'Description', 'MTU', 'Name', 'Type', 'NegoMode','Arp','DHCP', 'Mac', 
			    'WorkMode', 'ATMode', 'Able_BW', 'BandWidth', 'Able_Ping', 'Able_TraceRoute');
	
	foreach ($edit_key1 as $key)
	{
		$ret_array[$key] = GetXmlNetDevInterfaceInfo($dev, $key);
	}
	$ret_array['Description'] = base64_decode($ret_array['Description']);

	$ret_array["Address"] = GetXmlNetDevAddressInfo($dev);
	
	return $ret_array;
}

#Speed\nPhyState\nNegoMode\nUpDown??\nLinkMode\nMtu\nRcvdBytes\nTransdBytes\nIp\nMask\nDescription\nInBridge
function GetNetDevInfoByName($dev)
{
	$ret_array = array();
	$ret_array['Speed'] = GetNetDevSpeed($dev);
	$ret_array['Status'] = GetNetDevRunStatus($dev);
	$ret_array['PhyState'] = GetNetDevLinkStatus($dev);
	$ret_array['Nego'] = GetNetDevNegoMode($dev);
	$ret_array['LinkMode'] = GetNetDevLinkMode($dev);
	$ret_array['Mtu'] = GetNetDevMtu($dev);
	$ret_array['Mac'] = GetNetDevMac($dev);
	$tmp_array = GetNetDevRxTx($dev);
	$ret_array['RX'] = $tmp_array['RX'];
	$ret_array['TX'] = $tmp_array['TX'];
	$tmp_array = GetNetDevIpInfo($dev);
	$ret_array['IP'] = $tmp_array['IP'];
	$ret_array['MASK'] = $tmp_array['MASK'];
	$tmp_array = GetXmlNetDevInfo($dev);
 	$ret_array['Description'] = $tmp_array['Description'];
	$ret_array['Arp'] = $tmp_array['Arp'];
	$ret_array['DHCP'] = $tmp_array['DHCP'];
	$ret_array['WorkMode'] = $tmp_array['WorkMode'];
	$ret_array['ATMode'] = $tmp_array['ATMode'];
	$ret_array['Able_BW'] = $tmp_array['Able_BW'];
	$ret_array['BandWidth'] = $tmp_array['BandWidth'];
	$ret_array['Able_Ping'] = $tmp_array['Able_Ping'];
	$ret_array['Enabled'] = $tmp_array['Enabled'];
	$ret_array['Able_TraceRoute'] = $tmp_array['Able_TraceRoute'];
	return $ret_array;
}
#GetNetDevInfoByName('eth0');

function SetXmlNetDevInfoByName($in_array)
{	
	$edit_key = array();
	$ip_list = array();
	$dev = $in_array['Name'];
	$query_string = "/MINI/NETWORK/INTERFACES/INTERFACE[@Name='". $dev. "']";
	$edit_key['Enabled'] = $in_array['Enabled'];
	$edit_key['Description'] = base64_encode($in_array['Description']);
	$edit_key['MTU'] = $in_array['MTU'];
	$edit_key['Mac'] = $in_array['Mac'];
	$edit_key['DHCP'] = $in_array['DHCP'];
	$edit_key['WorkMode'] = $in_array['WorkMode'];
	$edit_key['ATMode'] = $in_array['ATMode'];
	$edit_key['NegoMode'] = $in_array['NegoMode'];
	$edit_key['Arp'] = $in_array['Arp'];
	$edit_key['Able_BW'] = $in_array['Able_BW'];
	
	$edit_key['BandWidth'] = $in_array['BandWidth'];
	
	$edit_key['Able_Ping'] = $in_array['Able_Ping'];
	$edit_key['Able_TraceRoute'] = $in_array['Able_TraceRoute'];
	EditAllAttrOfNode($query_string, $edit_key);

	if ($in_array['Ip'] != ''){
		$ip_list['Ip'] = $in_array['Ip'];
		$ip_list['Mask'] = $in_array['Mask'];
		$ip_list['Enabled'] = '1';
		
		$query_string = "/MINI/NETWORK/INTERFACES/INTERFACE[@Name='". $dev. "']/ADDRESSES/ADDRESS";
		$num = GetNumberOfNode($query_string);
		if ($num == 0){
			$query_string = "/MINI/NETWORK/INTERFACES/INTERFACE[@Name='". $dev. "']/ADDRESSES";
			$node_name = "ADDRESS";
			AppendAllAttrOfNode($query_string, $node_name, $ip_list);
		}
		else if ($num > 0){
			$query_string = "/MINI/NETWORK/INTERFACES/INTERFACE[@Name='". $dev. "']/ADDRESSES/ADDRESS";
			EditAllAttrOfNode($query_string, $ip_list);
		}	
	}
	ApplyNetDevInfoByName($dev);
	ApplyNetDevAddressByName($dev);
	ApplyNetDevRunStatus($dev);
}

function ApplyNetDevRunStatus($dev)
{
	$val_array = GetXmlNetDevInfo($dev);
	if ($val_array['Enabled'] == 1)
		$command_enabled = "/sbin/ifconfig ". $dev. " up &> /dev/null; ";
	if ($val_array['Enabled'] == 0)
		$command_enabled = "/sbin/ifconfig ". $dev. " down &> /dev/null; ";
	$ret_string['Enabled'] = shell_exec($command_enabled);

	return $ret_string;
}

function ClearAddressInfoByName($dev)
{
	$command = "ifconfig -a | grep ". $dev. " | awk '{printf $1 \" \"}'";
	$ret = shell_exec($command);
	$list = explode (" ", trim($ret));

	$command = "";
	foreach ($list as $l){
		$command .= "ifconfig ". $l. " 0;";	
	}
	shell_exec($command);
}

function ApplyNetDevAddressByName($dev)
{
	ClearAddressInfoByName($dev);
	
	$i = 0;
	$rc = 0;
	$ret_array = GetXmlNetDevInfo($dev);
	$ret_address = $ret_array['Address'];
	$ret_dhcp = $ret_array['DHCP'];
	
	$command_dhcp_kill = "/bin/kill -9  `/bin/ps | /bin/grep \"dhclient ". $dev. "\" | /bin/grep  \" S \" | /bin/awk '{printf $1 \" \" }'` >/dev/null 2>&1";
	//print $command_dhcp_kill;
	$ret_kill = shell_exec($command_dhcp_kill);

	if ($ret_array['DHCP'] == "1"){
		$command_string = "/usr/sbin/dhclient ". $dev. " 1>/dev/null 2>&1";
	}
	else {
		$command_string = "";
		$total_count = count($ret_address);
		
		foreach( $ret_address as $e){
			if ($e['Enabled'] == 1)
				$rc ++;
		}
		foreach( $ret_address as $r){
			if (($r['Enabled'] != 1)||($r['Ip'] == ''))
				continue;

			if ($rc > 1)
				$dev_array = " ". $dev. ":". $i ++;
			else
				$dev_array = $dev;

			$command_string .= "/sbin/ifconfig ". $dev_array. " ". $r['Ip'];
			
			if ($r['Mask'] != "") {
				$command_string .= " netmask ". $r['Mask'];
			}
			$command_string .= " &> /dev/null;";
		}
	}
	//print $command_string;
	$ret_string = shell_exec($command_string);
	return $ret_string;
}

function FlushSpecialNetDevArp($dev)
{
	$command = 
	"for i in `arp -an | grep ". $dev. " | awk '{printf $2}' |sed 's/)/ /g' | sed 's/(/ /g'`;". ' do arp -d $i; done ';
	$ret = shell_exec($command);
	return $ret;
}
function ApplyNetDevInfoByName($dev)
{
	$val_array = GetXmlNetDevInfo($dev);

	if ($val_array['Mac'] != ""){
		$command_mac  = "/sbin/ifconfig ". $dev. " down &>/dev/null; ";
		$command_mac .= "/sbin/ifconfig ". $dev. " hw ether " . $val_array['Mac']. " &> /dev/null; ";
		$command_mac .= "/sbin/ifconfig ". $dev. " up &>/dev/null; ";
		//print "\n". $command_mac. "\n";
		$ret_string['Mac'] = shell_exec($command_mac);
	}	

	if ($val_array['MTU'] != "")
	{
		$command_string .= "/sbin/ifconfig ". $dev. " mtu ". $val_array['MTU']. " &> /dev/null ;";
		//print "\n". $command_string. "\n";
		$ret_string['ifconfig'] = shell_exec($command_string);
	}

	$arp = $val_array['Arp'];
	if ($arp == "0" ) // disable arp auto find.
	{
		FlushSpecialNetDevArp($dev);
		$command_arp = "/sbin/ifconfig ". $dev. " down && ";
		$command_arp .= "/sbin/ifconfig ". $dev. " -arp && ";
		$command_arp .= "/sbin/ifconfig ". $dev. " up && ";
	}
	else
	{
		$command_arp = "/sbin/ifconfig ". $dev. " down && ";
		$command_arp .= "/sbin/ifconfig ". $dev. " arp && ";
		$command_arp .= "/sbin/ifconfig ". $dev. " up ";
	}
	//print "\n". $command_arp. "\n";
	$ret_string['arp'] = shell_exec($command_arp);

	if ($val_array['NegoMode'] != "") {
		$ethtool_string = "/usr/local/sbin/ethtool ";
		$ethtool_string2 = "autoneg off &>/dev/null ;";

		switch ($val_array['NegoMode']) {
		case 0:
			$nego = $ethtool_string. " -s ". $dev. " autoneg on &>/dev/null ;";
			break;
		case 1:
			$nego = $ethtool_string. " -s ". $dev. " speed 10 duplex half ". $ethtool_string2;
			break;
		case 2:
			$nego = $ethtool_string. " -s ". $dev. " speed 10 duplex full ". $ethtool_string2;
			break;
		case 3:
			$nego = $ethtool_string. " -s ". $dev. " speed 100 duplex half ". $ethtool_string2;
			break;
		case 4:
			$nego = $ethtool_string. " -s ". $dev. " speed 100 duplex full ". $ethtool_string2;
			break;
		case 6:
			$nego = $ethtool_string. " -s ". $dev. " speed 1000 duplex full ". $ethtool_string2;
			break;
		}
		//print "\n". $nego. "\n";
		$ret_string['ethtools'] = shell_exec($nego);
	}
	return $ret_string;
}

function ApplyNetDevInfo()
{
	$ret_array = GetXmlNetDevArray();
	foreach ($ret_array as $ret)
	{
		ApplyNetDevInfoByName($ret);
		ApplyNetDevAddressByName($ret);
	}
}

?>

